﻿ 
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--这个和我们的dao接口类的名称相同-->
<!--每次只需要修改包名-->
<mapper namespace="com.jiahe.rest.demo2.dao.idMapper">

	<!-- 开启缓存。数据库的所有操作都通过这个底层进行操作，可以打开缓存，否则可能一直读取的都是脏数据 -->

	<!-- 字段映射文件 -->
	<resultMap id="mDishesCook" type="DishesCook">
		<id column="id" property="Id"/>
		<result column="merchant_id" property="merchantId"/>
		<result column="name" property="name"/>
		<result column="sort_no" property="sortNo"/>
		<result column="cooking_type_id" property="cookingTypeId"/>
	</resultMap>

	<!-- insert 增加,注意数据库的名称以及子增长字段的赋值，然后再回传给上层对象-->
	<insert id="insert" parameterType="DishesCook">
		insert into dblxh.cy_dishes_cook
		(
			id,merchant_id,name,sort_no,cooking_type_id
		)
		values
		(
			#{id},#{merchantId},#{name},#{sortNo},#{cookingTypeId}
		)
		<selectKey resultType="Long" order="AFTER" keyProperty="id">
			SELECT LAST_INSERT_ID() AS id
		</selectKey>
	</insert>

		<!-- update 修改,注意set以及if test的使用从而有值的才修改-->
	<update id="update" parameterType="DishesCook">
  		update dblxh.cy_dishes_cook
  		<set>
			<if test="merchantId!=null">merchant_id=#{merchantId},</if>
			<if test="name!=null and name != ''">name=#{name},</if>
			<if test="sortNo!=null">sort_no=#{sortNo},</if>
			<if test="cookingTypeId!=null and cookingTypeId != ''">cooking_type_id=#{cookingTypeId},</if>
		</set>
  		<where>
  	  		id=#{id}
  		</where>
	</update>

	<!-- deleteByCondition 条件删除-->
	<delete id="deleteByCondition"  parameterType="DishesCook" >
		delete from dblxh.cy_dishes_cook 
		<include refid="ifWhere"/>
	</delete>
	<!-- deleteByID 删除-->
	<delete id="deleteByID"  parameterType="DishesCook" >
		delete from dblxh.cy_dishes_cook 
		where id=#{id}
	</delete>
	<!-- findByID 主键查询-->
	<select id="findByID" resultMap="mDishesCook" parameterType="DishesCook">
		select * from dblxh.cy_dishes_cook
		where id=#{id}
	</select>
	<!-- findByCondition 条件查询-->
	<select id="findByCondition" resultMap="mDishesCook" parameterType="DishesCook">
		select * from dblxh.cy_dishes_cook
		<include refid="ifWhere"/>
	</select>
	<!-- findCountByCondition 条件的数量-->
	<select id="findCountByCondition" resultType="Long" parameterType="DishesCook">
		select count(*) from dblxh.cy_dishes_cook
		<include refid="ifWhere"/>
	</select>

		<!-- ifWhere-->
	<sql id="ifWhere">
		<where>
			<if test="id!=null and id!=0">and id=#{id}</if>
			<if test="merchantId!=null and merchantId!=0">and merchant_id=#{merchantId}</if>
			<if test="name!=null and name!=''">and name=#{name}</if>
			<if test="sortNo!=null and sortNo!=0">and sort_no=#{sortNo}</if>
			<if test="cookingTypeId!=null and cookingTypeId!=''">and cooking_type_id=#{cookingTypeId}</if>
		</where>
	</sql>
</mapper>
